package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.ChannelDto;
import com.heima.model.wemedia.pojos.WmChannel;
import com.heima.model.wemedia.pojos.WmNews;
import com.heima.wemedia.mapper.WmChannelMapper;
import com.heima.wemedia.service.WmChannelService;
import com.heima.wemedia.service.WmNewsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.sql.Wrapper;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional
@Slf4j
public class WmChannelServiceImpl extends ServiceImpl<WmChannelMapper, WmChannel> implements WmChannelService {

    @Resource
    private WmChannelMapper wmChannelMapper;

    @Resource
    private WmNewsService wmNewsService;

    /**
     * 查询所有频道
     *
     * @return
     */
    @Override
    public ResponseResult findAll() {
        return ResponseResult.okResult(list());
    }

    /**
     * 根据条件模糊查询频道并且分页
     *
     * @return
     */
    @Override
    public ResponseResult findChannel(ChannelDto channelDto) {
        //1.判断参数是否存在
        if (channelDto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        LambdaQueryWrapper<WmChannel> queryWrapper = null;
        Page<WmChannel> page = new Page(channelDto.getPage(), channelDto.getSize());
        //2.判断name是否为空
        if (StringUtils.isNotBlank(channelDto.getName())) {
            queryWrapper = Wrappers.<WmChannel>lambdaQuery()
                    .like(WmChannel::getName, channelDto.getName());
            page = wmChannelMapper.selectPage(page, queryWrapper);
            return ResponseResult.okResult(page.getRecords());

        }
        page = wmChannelMapper.selectPage(page, null);
        return ResponseResult.okResult(page.getRecords());
    }

    /**
     * 根据id删除频道
     * @param id
     * @return
     */
    @Override
    public ResponseResult deleteChannelById(Integer id) {
        //1.校验参数是否符合条件
        if(id==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.判断改频道是否处于禁用状态
        WmChannel wmChannel = wmChannelMapper.selectById(id);
        Boolean status = wmChannel.getStatus();
        if(status){
            //该频道并未处于禁用状态
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"该频道未处于禁用状态");
        }
        //判断该频道是否被文章引用
        List<WmNews> list = wmNewsService.list(Wrappers.<WmNews>lambdaQuery().eq(WmNews::getChannelId, id));
        if(list!=null&&list.size()>0){
            //该频道已经被文章所引用
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"频道已被文章引用");
        }

        wmChannelMapper.deleteById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 增加频道
     * @param wmChannel
     * @return
     */
    @Override
    public ResponseResult addChannel(WmChannel wmChannel) {
        //1.判断参数是否存在
        if(wmChannel==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.查询所有频道，如果添加的频道是已经存在的频道，那么就不能继续添加
        List<WmChannel> wmChannelList = list(null);
        List<String> nameList = wmChannelList.stream().map(WmChannel::getName).collect(Collectors.toList());
        //如果存在，那么就不能够继续添加
        if(nameList.contains(wmChannel.getName())){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"该频道名字已经存在");
        }
        wmChannel.setCreatedTime(new Date());
        wmChannel.setIsDefault(true);
        save(wmChannel);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 修改频道信息
     * @return
     */
    @Override
    public ResponseResult updateChannel(WmChannel wmChannel) {
        //1.判断参数是否存在
        if(wmChannel==null){
            return ResponseResult.okResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //执行更新操作
        updateById(wmChannel);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}